﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SDT.Lib.Paging;
using System.Linq.Dynamic;
namespace SDT.Model
{
    public class OrderManager
    {

        public static IPagedList<Order> GetList(int payid,int status, string keyword, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new SDTDataContext();
            var l = dataContext.Orders
                .Where(a => keyword == string.Empty ? true : (a.OrderName.Contains(keyword.ToLower())) ||
                    (a.SeFullName.Contains(keyword.ToLower())) ||
                    (a.SeAddress.Contains(keyword.ToLower())) ||
                    (a.ReFullName.Contains(keyword.ToLower())) ||
                    (a.ReAddress.Contains(keyword.ToLower())) ||
                    (a.SePhone.Contains(keyword.ToLower())) ||
                    (a.RePhone.Contains(keyword.ToLower())))
                    .Where(e => payid > 0 ? e.PaymentID == payid : true)
                    .Where(e => status > 0 ? e.Status == status : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static IPagedList<Order> GetListInAdmin(int payid, int status,int typeid, DateTime fromDate, DateTime toDate, string keyword, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new SDTDataContext();
            if (fromDate == DateTime.MinValue || fromDate.Year < 1990) fromDate = new DateTime(1990, 1, 1);
            if (toDate == DateTime.MinValue || toDate.Year < 1990) toDate = new DateTime(1990, 1, 1);
            var l = dataContext.Orders
                .Where(a => keyword == string.Empty ? true : (a.OrderName.Contains(keyword.ToLower())) ||
                    (a.SeFullName.Contains(keyword.ToLower())) ||
                    (a.SeAddress.Contains(keyword.ToLower())) ||
                    (a.ReFullName.Contains(keyword.ToLower())) ||
                    (a.ReAddress.Contains(keyword.ToLower())) ||
                    (a.SePhone.Contains(keyword.ToLower())) ||
                    (a.RePhone.Contains(keyword.ToLower()))||
                    (a.EmsCode.Contains(keyword.ToLower()))||
                    (a.Customer.FullName.Contains(keyword.ToLower())) ||
                    (a.Note.Contains(keyword.ToLower())))
                    .Where(e => payid > 0 ? e.PaymentID == payid : true)
                    .Where(e => fromDate.Year>1990 ? e.AddDate >= fromDate : true)
                    .Where(e => toDate.Year>1990 ? e.AddDate <= toDate : true)
                    .Where(e => status > 0 ? e.Status == status : true)
                    .Where(e => typeid > 0 ? e.TypeID == typeid : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static IPagedList<Order> GetListOfCustomer(int customerid, int payid, int status, DateTime fromDate, DateTime toDate, string keyword, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new SDTDataContext();
            if (fromDate == DateTime.MinValue || fromDate.Year < 1990) fromDate = new DateTime(1990, 1, 1);
            if (toDate == DateTime.MinValue || toDate.Year < 1990) toDate = new DateTime(1990, 1, 1);
            var l = dataContext.Orders
                .Where(a => keyword == string.Empty ? true : (a.OrderName.Contains(keyword.ToLower())) ||
                    (a.SeFullName.Contains(keyword.ToLower())) ||
                    (a.SeAddress.Contains(keyword.ToLower())) ||
                    (a.ReFullName.Contains(keyword.ToLower())) ||
                    (a.ReAddress.Contains(keyword.ToLower())) ||
                    (a.SePhone.Contains(keyword.ToLower())) ||
                    (a.RePhone.Contains(keyword.ToLower())))
                    .Where(e => payid > 0 ? e.PaymentID == payid : true)
                    .Where(e => fromDate.Year > 1990 ? e.AddDate >= fromDate : true)
                    .Where(e => toDate.Year > 1990 ? e.AddDate <= toDate : true)
                    .Where(e => status > 0 ? e.Status == status : true)
                    .Where(e=>e.CustomerID==customerid)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static IList<Order> GetList(int payid, int status, string keyword, string order, string orderDir)
        {
            var dataContext = new SDTDataContext();
            var l = dataContext.Orders
                .Where(a => keyword == string.Empty ? true : (a.OrderName.Contains(keyword.ToLower())) ||
                    (a.SeFullName.Contains(keyword.ToLower())) ||
                    (a.SeAddress.Contains(keyword.ToLower())) ||
                    (a.ReFullName.Contains(keyword.ToLower())) ||
                    (a.ReAddress.Contains(keyword.ToLower())) ||
                    (a.SePhone.Contains(keyword.ToLower())) ||
                    (a.RePhone.Contains(keyword.ToLower())))
                    .Where(e => payid > 0 ? e.PaymentID == payid : true)
                    .Where(e => status > 0 ? e.Status == status : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).ToList();
            return l;
        }
        public static List<int> OperationDuration()
        {
            var dataContext = new SDTDataContext();
            var minDateTime = (from e in dataContext.Orders
                               select e.AddDate).Min();
            var maxDateTime = (from e in dataContext.Orders
                               select e.AddDate).Max();
            var minYear = minDateTime.Year;
            var maxYear = maxDateTime.Year;
            var returnList = new List<int>();
            for (int i = minYear; i <= maxYear; i++)
            {
                returnList.Add(i);
            }
            return returnList;
        }
        public static decimal Getshipping(List<Product> products, int number)
        {
            if (products == null || products.Count == 0) return 0;
            var maxShip = products.Select(e => e.FreeShipping).Max();
            
            if (maxShip == null) maxShip = 0;
            var dataContext = new SDTDataContext();
            if (number > 1)
            {
                var returnVal = dataContext.Shippings
                                .Where(e => e.ProductNumber == number)
                                .FirstOrDefault();
                maxShip = returnVal != null ? (double)returnVal.ShipValue : 0;
            }

            return (decimal)maxShip;
        }
    }
}
